package org.breezeiot.protodecode.protocol.qgdw3761.utils;

import org.breezeiot.protodecode.protocol.chargingpile.MsgDcsResult;
import org.breezeiot.protodecode.dataobject.gw.GwItemData;
import org.breezeiot.protodecode.dataobject.gw.afn0E.ErcItem;
import org.breezeiot.protodecode.dataobject.gw.afn0E.Req3rdDataResult;
import org.breezeiot.protodecode.utils.ProtocolGW_09Util;
import org.breezeiot.protodecode.utils.ProtocolUtils;
import org.breezeiot.protodecode.utils.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-09-20.
 */
@Component
public class Gw09ClassThreeUtil {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(Gw09ClassThreeUtil.class);

    public static MsgDcsResult parseClassThreeData(byte[] msg, int from, boolean acd) {
        String commAddr = String.format("%02X%02X%02X%02X", msg[8], msg[7], msg[10], msg[9]);
        int index = from;
        int fixlen = (msg[index] & 0x80) == 0 ? 2 : 8;
        if (acd) {
            fixlen += 2;
        }
        index++;
        MsgDcsResult gwResult = new MsgDcsResult();
        List<GwItemData> gwItemList = new ArrayList<GwItemData>();
        while ((index + fixlen) < msg.length) {
            Integer pn = ProtocolGW_09Util.getPnFromDA(msg[index + 1], msg[index]);
            int fn = ProtocolGW_09Util.getFnFromDT(msg[index + 3], msg[index + 2]);
            index += 4;
            GwItemData item = new GwItemData();
            item.setFn(fn);
            item.setPn(pn);
            if (fn == 1 || fn == 2) {
                Req3rdDataResult reqDataComm = new Req3rdDataResult();
                List<ErcItem> ercitemlist = new ArrayList<ErcItem>();
                int endPoint = ProtocolUtils.Unsignd(msg[index + 3]);//Pn
                int startPoint = ProtocolUtils.Unsignd(msg[index + 2]);//Pm
                int num = 0;
                if (startPoint < endPoint) {//当Pm＜Pn时，Y= Pn-Pm
                    num = endPoint - startPoint;
                }
                if (startPoint > endPoint) {//当Pm＞Pn时，Y= 256＋Pn-Pm
                    num = 256 + endPoint - startPoint;
                }
                index += 4;
                for (int k = 0; k < num; k++) {
                    ErcItem ercitem = new ErcItem();
                    int erc = ProtocolUtils.Unsignd(msg[index++]);// 事件代码ERC
                    ercitem.setErc(erc);
                    switch (erc) {
                        case 1:// 数据初始化和版本变更记录
                            ercitem = parseERC1Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 2:// 数据初始化和版本变更记录
                            ercitem = parseERC2Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 3:// 参数变更记录
                            ercitem = parseERC3Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 4:// 状态量变位记录
                            ercitem = parseERC4Frame(msg, index, ercitem);
                            ercitemlist.add(ercitem);
                            break;
                        case 5:// 遥控跳闸记录
                            ercitem = parseERC5Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 6:// 功控跳闸记录
                            ercitem = parseERC6Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 7:// 电控跳闸记录
                            ercitem = parseERC7Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 8:// 电能表参数变更
                            ercitem = parseERC8Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 9:// 电流回路异常
                            ercitem = parseERC9Frame(msg, index, ercitem);
                            ercitemlist.add(ercitem);
                            break;
                        case 10:// 电压回路异常
                            ercitem = parseERC10Frame(msg, index, ercitem);
                            ercitemlist.add(ercitem);
                            break;
                        case 11:// 相序异常
                            ercitem = parseERC11Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 12:// 电能表时间超差
                            ercitem = parseERC12Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 13:// 电表故障信息
                            ercitem = parseERC13Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 14:// 终端停/上电事件
                            ercitem = parseERC14Frame(msg, index, ercitem);
                            ercitemlist.add(ercitem);
                            break;
                        case 15:// 谐波越限告警
                            ercitem = parseERC15Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 17:// 电压/电流不平衡度越限记录
                            ercitem = parseERC17Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 18:// 电容器投切自锁记录
                            ercitem = parseERC18Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 19:// 购电参数设置记录
                            ercitem = parseERC19Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 20:// 消息认证错误记录
                            ercitem = parseERC20Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 21:// 终端故障记录
                            ercitem = parseERC21Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 22:// 有功总电能量差动越限事件记录
                            ercitem = parseERC22Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 23:// 电控告警事件记录
                            ercitem = parseERC23Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 24:// 电压越限记录
                            ercitem = parseERC24Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 25:// 电流越限记录
                            ercitem = parseERC25Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 26:// 视在功率越限记录
                            ercitem = parseERC26Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 27:// 电能表示度下降记录
                            ercitem = parseERC27Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 28:// 电能量超差记录
                            ercitem = parseERC28Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 29:// 电能表飞走记录
                            ercitem = parseERC29Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 30:// 电能表停走记录
                            ercitem = parseERC30Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 31:// 终端485抄表失败事件记录
                            ercitem = parseERC31Frame(msg, index, ercitem);
                            ercitemlist.add(ercitem);
                            break;
                        case 32:// 终端与主站通信流量超门限事件记录
                            ercitem = parseERC32Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 33:// 电能表运行状态字变位事件记录
                            ercitem = parseERC33Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 34:// CT异常事件记录
                            ercitem = parseERC34Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                        case 35:// 发现未知电表事件记录
                            ercitem = parseERC35Frame(msg, index, ercitem);//
                            ercitemlist.add(ercitem);
                            break;
                    }
                    int le = ProtocolUtils.Unsignd(msg[index++]);//事件记录长度Le
                    index += le;
                }
                reqDataComm.setErcitemlist(ercitemlist);
                item.setData(reqDataComm);
                item.setSuccess(true);
            }
            gwItemList.add(item);
        }
        gwResult.setData(gwItemList);
        gwResult.setSuccess(true);
        gwResult.setMessage("成功");
        return gwResult;
    }

    private static String getEventData(byte[] byteValue, int index) {
        StringBuffer eventdata = new StringBuffer("");
        eventdata.append("Ua/Uab:" + ProtocolGW_09Util.parseA7(byteValue, index + 1).toString() + ";");
        eventdata.append("Ub:" + ProtocolGW_09Util.parseA7(byteValue, index + 3).toString() + ";");
        eventdata.append("Uc/Ucb:" + ProtocolGW_09Util.parseA7(byteValue, index + 5).toString() + ";");
        eventdata.append("Ia:" + ProtocolGW_09Util.parseA25(byteValue, index + 7).toString() + ";");
        eventdata.append("Ib:" + ProtocolGW_09Util.parseA25(byteValue, index + 10).toString() + ";");
        eventdata.append("Ic:" + ProtocolGW_09Util.parseA25(byteValue, index + 13).toString() + ";");
        eventdata.append("zy:" + ProtocolGW_09Util.parseA14(byteValue, index + 16).toString() + ";");
        return eventdata.toString();
    }

    private static ErcItem parseERC2Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        int d0 = (byteValue[index] & 0x01);
        resultString.append("事件标志:").append((d0 == 1 ? "终端参数丢失" : "终端参数未丢失")).append(";");
        int d1 = ((byteValue[index] >> 1) & 0x01);
        resultString.append((d0 == 1 ? "测量点参数丢失" : "测量点参数未丢失")).append(";");
        index++;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC3Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("启动站地址:").append(ProtocolUtils.Unsignd(byteValue[index])).append(";");
        int num = (le - 6) / 4;
        for (int i = 0; i < num; i++) {
            resultString.append("变更参数数据单元标识:").append((i + 1) + ProtocolUtils.byteToInteger(byteValue, index)).append(";");
            index = index + 4;
        }
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC4Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("状态变位:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("变位后状态:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC5Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("跳闸时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("跳闸轮次:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("跳闸时功率（总加功率）:").append(ProtocolGW_09Util.parseA2(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("跳闸后2分钟的功率（总加功率）:").append(ProtocolGW_09Util.parseA2(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC6Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("跳闸时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("pn（总加组号）:").append(StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 6, "0")).append(";");// pn（总加组号)D0～D5
        resultString.append("跳闸轮次:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("功控类别:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("跳闸前功率（总加功率）:").append(ProtocolGW_09Util.parseA2(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("跳闸后2分钟的功率（总加功率）:").append(ProtocolGW_09Util.parseA2(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("跳闸时功率定值:").append(ProtocolGW_09Util.parseA2(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC7Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("跳闸时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("pn（总加组号）:").append(StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 6, "0")).append(";");// pn（总加组号)D0～D5
        resultString.append("跳闸轮次:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("电控类别:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("跳闸时电能量（总加电能量）:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("跳闸时电能量定值:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC8Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int d0 = (byteValue[index] & 0x01);
        resultString.append((d0 == 1 ? "电能表费率时段变化" : "电表费率时段无变更")).append(";");
        int d1 = ((byteValue[index] & 0x02) >> 1);
        resultString.append((d1 == 1 ? "电能表编程时间更改" : "电能表编程时间未更改")).append(";");
        int d2 = ((byteValue[index] & 0x04) >> 2);
        resultString.append((d2 == 1 ? "电能表编程时间更改" : "电能表编程时间未更改")).append(";");
        int d3 = ((byteValue[index] & 0x08) >> 3);
        resultString.append((d3 == 1 ? "电能表脉冲常数更改" : "电能表脉冲常数未更改")).append(";");
        int d4 = ((byteValue[index] & 0x10) >> 4);
        resultString.append((d4 == 1 ? "电能表的互感器倍率更改" : "电能表的互感器倍率未更改")).append(";");
        int d5 = ((byteValue[index] & 0x20) >> 5);
        resultString.append((d5 == 1 ? "电能表最大需量清零" : "电能表最大需量未被清零")).append(";");
        index++;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC9Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("异常标志:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("发生时的Ua/Uab:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ub:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Uc/Ucb:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ia:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时的Ib:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时的Ic:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时电能表正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC10Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("异常标志:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        resultString.append("发生时的Ua/Uab:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ub:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Uc/Ucb:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ia:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时的Ib:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时的Ic:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时电能表正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC11Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("∠Ua/Uab(度):").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("∠Ub (度):").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("∠Uc/Ucb(度):").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("∠Ia (度):").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("∠Ib(度):").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("∠Ic(度):").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时电能表正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC12Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC13Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int d0 = (byteValue[index] & 0x01);
        resultString.append((d0 == 1 ? "电能表编程次数或最大需量清零次数发生变化" : "电能表编程次数及最大需量清零次数无变化")).append(";");
        int d1 = ((byteValue[index] & 0x02) >> 1);
        resultString.append((d1 == 1 ? "电能表断相次数变化" : "电能表断相次数无变化")).append(";");
        int d2 = ((byteValue[index] & 0x04) >> 2);
        resultString.append((d2 == 1 ? "电能表失压次数变化" : "电能表失压次数无变化")).append(";");
        int d3 = ((byteValue[index] & 0x08) >> 3);
        resultString.append((d3 == 1 ? "电能表停电次数变化" : "电能表停电次数无变化")).append(";");
        int d4 = ((byteValue[index] & 0x10) >> 4);
        resultString.append((d4 == 1 ? "电能表电池欠压" : "电能表电池不欠压")).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC14Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        index++;//属性标志
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("停电发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("上电时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC15Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int abnormalFlag = ProtocolUtils.Unsignd((byte) ((byteValue[index] >> 7) & 0x01));// 异常标志
        if (abnormalFlag == 0) {
            resultString.append("谐波电压越限事件:");
            int Ua = byteValue[index] & 0x01;
            resultString.append((Ua == 1 ? "Uab/Ua发生异常" : "无")).append(";");
            int Ub = ((byteValue[index] >> 1) & 0x01);
            resultString.append((Ub == 1 ? "Ub发生异常" : "无")).append(";");
            int Uc = ((byteValue[index] >> 2) & 0x01);
            resultString.append((Uc == 1 ? "Ucb/Uc发生异常" : "无")).append(";");
        } else if (abnormalFlag == 0) {
            resultString.append("谐波电流越限事件:");
            int Ia = byteValue[index] & 0x01;
            resultString.append((Ia == 1 ? "Iab/Ia发生异常" : "无")).append(";");
            int Ib = ((byteValue[index] >> 1) & 0x01);
            resultString.append((Ib == 1 ? "Ib发生异常" : "无")).append(";");
            int Ic = ((byteValue[index] >> 2) & 0x01);
            resultString.append((Ic == 1 ? "Icb/Ic发生异常" : "无")).append(";");
        }
        resultString.append("谐波越限标志:").append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0"));
        resultString.append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0"));
        resultString.append(StringUtil.AddjustLength(Integer.toBinaryString(ProtocolUtils.Unsignd(byteValue[index++])), 8, "0")).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC17Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int overUFlag = ProtocolUtils.Unsignd((byte) ((byteValue[index]) & 0x01));
        resultString.append((overUFlag == 1 ? "电压不平衡度越限" : "")).append(";");
        int overIFlag = ProtocolUtils.Unsignd((byte) ((byteValue[index] >> 1) & 0x01));
        resultString.append((overIFlag == 1 ? "电流不平衡度越限" : "")).append(";");
        resultString.append("发生时的电压不平衡度（％）:").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的电流不平衡度（％）:").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ua/Uab:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ub:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Uc/Ucb:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ia:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ib:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ic:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC18Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("pn(测量点号):").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("异常标志:");
        int flag1 = ProtocolUtils.Unsignd((byte) ((byteValue[index]) & 0x01));
        resultString.append((flag1 == 1 ? "过压" : "")).append(";");
        int flag2 = ProtocolUtils.Unsignd((byte) ((byteValue[index] >> 1) & 0x01));
        resultString.append((flag2 == 1 ? "装置故障" : "")).append(";");
        int flag3 = ProtocolUtils.Unsignd((byte) ((byteValue[index] >> 2) & 0x01));
        resultString.append((flag3 == 1 ? "执行回路故障" : "")).append(";");
        int flag = ProtocolUtils.Unsignd((byte) (byteValue[index]));// 电容器组标志
        resultString.append("该电容器组").append((flag == 1 ? "自锁" : "未自锁"));
        index = index + 2;
        resultString.append("越限发生时功率因数:").append(ProtocolGW_09Util.parseA5(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("越限发生时无功功率:").append(ProtocolGW_09Util.parseA23(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("越限发生时电压:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC19Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("购电能量设置时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("pn(总加组号):").append(ProtocolUtils.Unsignd((byte) (byteValue[index++] & 0x3F))).append(";");
        index = index + 5;
        resultString.append("购电单号:").append(ProtocolUtils.Unsignd(byteValue[index]));
        index = index + 4;
        int flag = ProtocolUtils.Unsignd((byte) (byteValue[index]));
        if (flag == 55) {
            resultString.append("追加标志;");
        } else if (Integer.valueOf(flag).toString() == "AA") {
            resultString.append("刷新标志;");
        }
        resultString.append("购电量值:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("报警门限:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("跳闸门限:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("本次购电前剩余电能量（费）:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("本次购电后剩余电能量（费）:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    /**
     *
     */
    private static ErcItem parseERC35Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("终端通信端口号:").append(ProtocolUtils.Unsignd((byte) (byteValue[index++] & 0x3F))).append(";");
        int n = ProtocolUtils.Unsignd(byteValue[index++]);
        resultString.append("发现块数:").append(n).append(";");
        for (int i = 0; i < n; i++) {
            resultString.append("第").append((i + 1)).append("块未知电表通信地址:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
            index = index + 6;
            resultString.append("第").append((i + 1)).append("块未知电表所在相别:");
            if (((byteValue[index] >> 5) & 0x01) == 1) {
                resultString.append("A相");
            }
            if (((byteValue[index] >> 6) & 0x01) == 1) {
                resultString.append("、B相");
            }
            if (((byteValue[index] >> 7) & 0x01) == 1) {
                resultString.append("、C相");
            }
            resultString.append(";");
            resultString.append("第").append((i + 1)).append("块未知电表发现者接收到的信号品质:").append(ProtocolUtils.Unsignd((byte) (byteValue[index] & 0x0F))).append(";");
            index++;
            int protocoltpye = ProtocolUtils.Unsignd((byte) (byteValue[index++] & 0x03));
            if (protocoltpye == 0) {
                resultString.append("第").append((i + 1)).append("块未知电表通信协议:").append("DL/T645-1997").append(";");
            } else if (protocoltpye == 1) {
                resultString.append("第").append((i + 1)).append("块未知电表通信协议:").append("DL/T645-2007").append(";");
            }
        }

        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC34Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int errtype = ProtocolUtils.Unsignd((byte) ((byteValue[index] >> 6) & 0x03));
        if (errtype == 1) {
            resultString.append("编码表示异常类型:").append("一次侧短路").append(";");
        } else if (errtype == 2) {
            resultString.append("编码表示异常类型:").append("二次侧短路").append(";");
        } else if (errtype == 3) {
            resultString.append("编码表示异常类型:").append("二次侧开路").append(";");
        }
        resultString.append("相位(A,B,C):").append(StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index] & 0x07), 3, "0")).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC33Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        // 电表运行状态字变位标志1
        resultString.append("电表运行状态字变位标志1:");
        int jisuan = ((byteValue[index] >> 1) & 0x01);
        resultString.append("需量积算方式:").append(jisuan).append(";");
        int shizhong = ((byteValue[index] >> 2) & 0x01);
        resultString.append("时钟电池:").append(shizhong).append(";");
        int dingdian = ((byteValue[index] >> 3) & 0x01);
        resultString.append("停电抄表电池:").append(dingdian).append(";");
        int ygfangxiang = ((byteValue[index] >> 4) & 0x01);
        resultString.append("有功功率方向:").append(ygfangxiang).append(";");
        int wgfangxiang = ((byteValue[index] >> 5) & 0x01);
        resultString.append("无功功率方向:").append(wgfangxiang).append(";");
        index = index + 2;
        // 电表运行状态字变位标志2
        resultString.append("电表运行状态字变位标志2:");
        int yougongA = byteValue[index] & 0x01;
        resultString.append("A相有功功率方向:").append(yougongA).append(";");
        int yougongB = ((byteValue[index] >> 1) & 0x01);
        resultString.append("B相有功功率方向:").append(yougongB).append(";");
        int yougongC = ((byteValue[index] >> 2) & 0x01);
        resultString.append("C相有功功率方向:").append(yougongC).append(";");
        int wugongA = ((byteValue[index] >> 4) & 0x01);
        resultString.append("A相无功功率方向:").append(wugongA).append(";");
        int wugongB = ((byteValue[index] >> 5) & 0x01);
        resultString.append("B相无功功率方向:").append(wugongB).append(";");
        int wugongC = ((byteValue[index] >> 6) & 0x01);
        resultString.append("C相无功功率方向:").append(wugongC).append(";");
        index = index + 2;
        // 电表运行状态字变位标志3
        resultString.append("电表运行状态字变位标志3:");
        int yunxing = byteValue[index] & 0x01;
        resultString.append("当前运行时段:").append(yunxing).append(";");
        int gongdian = ((byteValue[index] >> 1) & 0x03);
        resultString.append("供电方式:").append(gongdian).append(";");
        int buancheng = ((byteValue[index] >> 3) & 0x01);
        resultString.append("编程允许:").append(buancheng).append(";");
        int jidianqi = ((byteValue[index] >> 4) & 0x01);
        resultString.append("继电器状态:").append(jidianqi).append(";");
        index = index + 2;
        // 电表运行状态字变位标志4,5,6
        for (int i = 4; i <= 6; i++) {
            resultString.append("电表运行状态字变位标志字").append(i).append(":");
            int shiya = byteValue[index] & 0x01;
            resultString.append("失压:").append(shiya).append(";");
            int qianya = ((byteValue[index] >> 1) & 0x01);
            resultString.append("欠压:").append(qianya).append(";");
            int guoya = ((byteValue[index] >> 2) & 0x01);
            resultString.append("过压:").append(guoya).append(";");
            int shiliu = ((byteValue[index] >> 3) & 0x01);
            resultString.append("失流:").append(shiliu).append(";");
            int guoliu = ((byteValue[index] >> 4) & 0x01);
            resultString.append("过流:").append(guoliu).append(";");
            int guozai = ((byteValue[index] >> 5) & 0x01);
            resultString.append("过载:").append(guozai).append(";");
            int fanxiang = ((byteValue[index] >> 6) & 0x01);
            resultString.append("反向:").append(fanxiang).append(";");
            int duanxiang = ((byteValue[index] >> 7) & 0x01);
            resultString.append("断相:").append(duanxiang).append(";");
            index = index + 2;
        }
        // 电表运行状态字变位标志7
        resultString.append("电表运行状态字变位标志7:");
        int dianyani = byteValue[index] & 0x01;
        resultString.append("电压逆相序:").append(dianyani).append(";");
        int dianliuni = ((byteValue[index] >> 1) & 0x01);
        resultString.append("电流逆相序:").append(dianliuni).append(";");
        int dianyapin = ((byteValue[index] >> 2) & 0x01);
        resultString.append("电压不平衡:").append(dianyapin).append(";");
        int dianliupin = ((byteValue[index] >> 3) & 0x01);
        resultString.append("电流不平衡:").append(dianliupin).append(";");
        index = index + 2;
        /**
         * 电表运行状态字
         */
        resultString.append("电表运行状态字1:");
        jisuan = ((byteValue[index] >> 1) & 0x01);
        resultString.append("需量积算方式:").append(jisuan).append(";");
        shizhong = ((byteValue[index] >> 2) & 0x01);
        resultString.append("时钟电池:").append(shizhong).append(";");
        dingdian = ((byteValue[index] >> 3) & 0x01);
        resultString.append("停电抄表电池:").append(dingdian).append(";");
        ygfangxiang = ((byteValue[index] >> 4) & 0x01);
        resultString.append("有功功率方向:").append(ygfangxiang).append(";");
        wgfangxiang = ((byteValue[index] >> 5) & 0x01);
        resultString.append("无功功率方向:").append(wgfangxiang).append(";");
        index = index + 2;
        // 电表运行状态字2
        resultString.append("电表运行状态字2:");
        yougongA = byteValue[index] & 0x01;
        resultString.append("A相有功功率方向:").append(yougongA).append(";");
        yougongB = ((byteValue[index] >> 1) & 0x01);
        resultString.append("B相有功功率方向:").append(yougongB).append(";");
        yougongC = ((byteValue[index] >> 2) & 0x01);
        resultString.append("C相有功功率方向:").append(yougongC).append(";");
        wugongA = ((byteValue[index] >> 4) & 0x01);
        resultString.append("A相无功功率方向:").append(wugongA).append(";");
        wugongB = ((byteValue[index] >> 5) & 0x01);
        resultString.append("B相无功功率方向:").append(wugongB).append(";");
        wugongC = ((byteValue[index] >> 6) & 0x01);
        resultString.append("C相无功功率方向:").append(wugongC).append(";");
        index = index + 2;
        // 电表运行状态字3
        resultString.append("电表运行状态字3:");
        yunxing = byteValue[index] & 0x01;
        resultString.append("当前运行时段:").append(yunxing).append(";");
        gongdian = ((byteValue[index] >> 1) & 0x03);
        resultString.append("供电方式:").append(gongdian).append(";");
        buancheng = ((byteValue[index] >> 3) & 0x01);
        resultString.append("编程允许:").append(buancheng).append(";");
        jidianqi = ((byteValue[index] >> 4) & 0x01);
        resultString.append("继电器状态:").append(jidianqi).append(";");
        index = index + 2;
        // 电表运行状态字4,5,6
        for (int i = 4; i <= 6; i++) {
            resultString.append("电表运行状态字").append(i).append(":");
            int shiya = byteValue[index] & 0x01;
            resultString.append("失压:").append(shiya).append(";");
            int qianya = ((byteValue[index] >> 1) & 0x01);
            resultString.append("欠压:").append(qianya).append(";");
            int guoya = ((byteValue[index] >> 2) & 0x01);
            resultString.append("过压:").append(guoya).append(";");
            int shiliu = ((byteValue[index] >> 3) & 0x01);
            resultString.append("失流:").append(shiliu).append(";");
            int guoliu = ((byteValue[index] >> 4) & 0x01);
            resultString.append("过流:").append(guoliu).append(";");
            int guozai = ((byteValue[index] >> 5) & 0x01);
            resultString.append("过载:").append(guozai).append(";");
            int fanxiang = ((byteValue[index] >> 6) & 0x01);
            resultString.append("反向:").append(fanxiang).append(";");
            int duanxiang = ((byteValue[index] >> 7) & 0x01);
            resultString.append("断相:").append(duanxiang).append(";");
            index = index + 2;
        }
        // 电表运行状态字7
        resultString.append("电表运行状态字7:");
        dianyani = byteValue[index] & 0x01;
        resultString.append("电压逆相序:").append(dianyani).append(";");
        dianliuni = ((byteValue[index] >> 1) & 0x01);
        resultString.append("电流逆相序:").append(dianliuni).append(";");
        dianyapin = ((byteValue[index] >> 2) & 0x01);
        resultString.append("电压不平衡:").append(dianyapin).append(";");
        dianliupin = ((byteValue[index] >> 3) & 0x01);
        resultString.append("电流不平衡:").append(dianliupin).append(";");
        index = index + 2;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC32Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("当月已发生的通信流量:").append(ProtocolUtils.byteToInteger(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("月通信流量门限:").append(ProtocolUtils.byteToInteger(byteValue, index)).append(";");
        index = index + 4;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC31Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("最近一次抄表成功时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("最近一次抄表成功正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("最近一次抄表成功正向无功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC30Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("电能表停走发生时正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("电能表停走阈值:").append(ProtocolUtils.Unsignd(byteValue[index])).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC29Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("电能表飞走发生前正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("电能表飞走发生后正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("电能表飞走阈值:").append(ProtocolGW_09Util.parseA22(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC28Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("电能量超差发生时对应正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("电能量超差发生时正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("电能量超差阈值:").append(ProtocolGW_09Util.parseA22(byteValue, index)).append(";");
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC27Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        resultString.append("下降前电能表正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("下降前电能表正向有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
        index = index + 5;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC26Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int overType = ((byteValue[index] >> 6) & 0x03);
        if (overType == 1) {
            resultString.append("越限标志:越视在功率上上限;");
        } else if (overType == 2) {
            resultString.append("越限标志:越视在功率上限;");
        }
        index++;
        resultString.append("发生时的视在功率:").append(ProtocolGW_09Util.parseA23(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时的视在功率限值:").append(ProtocolGW_09Util.parseA23(byteValue, index)).append(";");
        index = index + 3;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC25Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int overType = ((byteValue[index] >> 6) & 0x03);
        if (overType == 1) {
            resultString.append("越限标志:越视在电流上上限;");
        } else if (overType == 2) {
            resultString.append("越限标志:越视在电流上限;");
        }
        resultString.append("越限相(A,B,C):").append(StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index] & 0x07), 3, "0"));
        index++;
        resultString.append("发生时的Ia:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时的Ib:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        resultString.append("发生时的Ic:").append(ProtocolGW_09Util.parseA25(byteValue, index)).append(";");
        index = index + 3;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC24Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index + 1] >> 7) & 0x01))).append(";");
        String pn = StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index + 1]), 4, "0") + StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index]), 8, "0");
        resultString.append("测量点号:").append(Integer.valueOf(pn, 2).toString()).append(";");
        index = index + 2;
        int overType = ((byteValue[index] >> 6) & 0x03);
        if (overType == 1) {
            resultString.append("越限标志:越视在电压上上限;");
        } else if (overType == 2) {
            resultString.append("越限标志:越视在电压上限;");
        }
        resultString.append("越限相(A,B,C):").append(StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index] & 0x07), 3, "0"));
        index++;
        resultString.append("发生时的Ua/Uab:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Ub:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        resultString.append("发生时的Uc/Ucb:").append(ProtocolGW_09Util.parseA7(byteValue, index)).append(";");
        index = index + 2;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC23Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("告警时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("总加组号:").append(ProtocolUtils.Unsignd((byte) (byteValue[index++] & 0x3F))).append(";");
        resultString.append("投入轮次:").append(StringUtil.AddjustLength(Integer.toBinaryString(byteValue[index++]), 8, "0")).append(";");
        int type = ProtocolUtils.Unsignd(byteValue[index++]);
        resultString.append("电控类别:").append((type == 1 ? "月电控" : "购电控")).append(";");
        resultString.append("告警时电能量:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("告警时电控定值:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC22Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("起止标志:").append(ProtocolUtils.Unsignd((byte) ((byteValue[index] >> 7) & 0x01))).append(";");
        resultString.append("电能量差动组号:").append(ProtocolUtils.Unsignd((byte) (byteValue[index++] & 0x3F))).append(";");
        resultString.append("越限时对比总加组有功总电能量:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("越限时参照总加组有功总电能量:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        resultString.append("越限时差动越限相对偏差值:").append(ProtocolUtils.Unsignd(byteValue[index++])).append(";");
        resultString.append("越限时差动越限绝对偏差值:").append(ProtocolGW_09Util.parseA3(byteValue, index)).append(";");
        index = index + 4;
        int n = ProtocolUtils.Unsignd(byteValue[index++]);
        resultString.append("对比的总加组测量点数量:").append(n).append(";");
        for (int i = 0; i < n; i++) {
            resultString.append("越限时对比总加组第").append((i + 1)).append("测量点有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
            index = index + 5;
        }
        int n2 = ProtocolUtils.Unsignd(byteValue[index++]);
        resultString.append("对比的总加组测量点数量:").append(n2).append(";");
        for (int i = 0; i < n2; i++) {
            resultString.append("越限时对比总加组第").append((i + 1)).append("测量点有功总电能示值:").append(ProtocolGW_09Util.parseA14(byteValue, index)).append(";");
            index = index + 5;
        }
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC21Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        int type = ProtocolUtils.Unsignd(byteValue[index++]);
        switch (type) {
            case 1:
                resultString.append("终端故障编码:终端主板内存故障;");
                break;
            case 2:
                resultString.append("终端故障编码:时钟故障;");
                break;
            case 3:
                resultString.append("终端故障编码:主板通信故障;");
                break;
            case 4:
                resultString.append("终端故障编码:485抄表故障;");
                break;
            case 5:
                resultString.append("终端故障编码:显示板故障;");
                break;
            case 6:
                resultString.append("终端故障编码:载波通道异常;");
                break;
        }
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC20Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("发生时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");
        index = index + 5;
        resultString.append("消息认证码PW:");
        String s = "";
        for (int i = 0; i < 16; i++) {
            s = ProtocolUtils.Unsignd(byteValue[index++]) + s;
        }
        resultString.append("启动站地址MSA:").append(ProtocolUtils.Unsignd(byteValue[index++]));
        ercitem.setValue(resultString.toString());
        return ercitem;
    }

    private static ErcItem parseERC1Frame(byte[] byteValue, int index, ErcItem ercitem) {
        StringBuffer resultString = new StringBuffer("");
        int le = ProtocolUtils.Unsignd(byteValue[index++]);// 长度le
        resultString.append("初始化/版本变更时间:").append(ProtocolGW_09Util.parseA15(byteValue, index)).append(";");// 初始化/版本变更时间:yyyy-MM-dd
        // HH:mm;
        index = index + 5;
        int rtuparam = (byteValue[index] & 0x01);
        resultString.append("终端进行参数及数据区初始化:").append((rtuparam == 1 ? "发生初始化" : "无初始化发生")).append(";");
        int rtuver = ((byteValue[index] >> 1) & 0x01);
        resultString.append("终端版本变更:").append((rtuver == 1 ? "发生变更" : "无变更")).append(";");
        index++;
        StringBuffer version = new StringBuffer("");
        for (int i = 0; i < 4; i++) {
            if (byteValue[index + i] != 0) {
                version.append((char) byteValue[index + i]);
            }
        }
        resultString.append("变更前软件版本号:").append(version.toString()).append(";");
        index = index + 4;
        version = new StringBuffer("");
        for (int i = 0; i < 4; i++) {
            if (byteValue[index + i] != 0) {
                version.append((char) byteValue[index + i]);
            }
        }
        resultString.append("变更后软件版本号:").append(version.toString()).append(";");
        index = index + 4;
        ercitem.setValue(resultString.toString());
        return ercitem;
    }
}
